#include "stdafx.h"
#include "RF625DeviceNet.h"
#include "include\RF625Device.h"
#include "include\RFDeviceDefs.h"

using namespace RFDeviceNet;
using namespace System::Runtime::InteropServices;

RF625DeviceNet^ RF625DeviceNet::CreateRF625Device()
{
	RFDevice::RF625Device* device = new RFDevice::RF625Device();
	RF625DeviceNet^ dev = gcnew RF625DeviceNet(device);
	return dev;
}

RF625DeviceNet^ RF625DeviceNet::CreateRF625Device(_UDP_DEVINFOBLOCK_PC_Net^ devBlock)
{
	RFDevice::_UDP_DEVINFOBLOCK_PC* db = new RFDevice::_UDP_DEVINFOBLOCK_PC();
	devBlock->GetDevBlock(db);
	RFDevice::RF625Device* device = new RFDevice::RF625Device(db);
	RF625DeviceNet^ dev = gcnew RF625DeviceNet(device);
	return dev;
}

RF625DeviceNet::RF625DeviceNet()
{
	_rf625Device = new RF625Device();	
}

RF625DeviceNet::RF625DeviceNet(RF625Device* device) : RFLanDeviceNet(device)
{
	_rf625Device = device;
}

RF625DeviceNet::~RF625DeviceNet()
{
	_rf625Device->~RF625Device();
	_rf625Device = NULL;
}

bool RF625DeviceNet::Connect(){
	bool ret = _rf625Device->Connect();	
	return ret;
}


bool RF625DeviceNet::Connect(_UDP_DEVINFOBLOCK_PC_Net^ devBlock )
{
	_UDP_DEVINFOBLOCK_PC* db = new _UDP_DEVINFOBLOCK_PC();
	devBlock->GetDevBlock(db);
	bool ret = _rf625Device->Connect(db);
	return ret;
}

bool RF625DeviceNet::Disconnect(){
	return _rf625Device->Disconnect();
}

bool RF625DeviceNet::GetResult(array<byte>^% buffer, bool OldMode){
	//return _rf625Device->GetResult((void*)buffer);
	char* p = new char[ProfileBufferSize];
	bool ret = _rf625Device->GetResult(p, OldMode = false);
	buffer = gcnew array<byte>(ProfileBufferSize);
	for (int i = 0; i < ProfileBufferSize; i ++)
	{
		buffer[i] = p[i];
	}
	return ret;
}

bool RF625DeviceNet::GetImage(array<byte>^% buffer){
	char* p = new char[ImageBufferSize];
	bool ret = _rf625Device->GetImage(p);
	buffer = gcnew array<byte>(ImageBufferSize);
	for (int i = 0; i < ImageBufferSize; i ++)
	{
		buffer[i] = p[i];
	}
	return ret;
}

short RF625DeviceNet::GetResultInclinationAngle()
{
	return _rf625Device->GetResultInclinationAngle();
}

bool RF625DeviceNet::ReadParams()
{
	return _rf625Device->ReadParams();
}

bool RF625DeviceNet::WriteParams()
{
	return _rf625Device->WriteParams();
}

bool RF625DeviceNet::FlushParams(bool bDefault)
{
	return _rf625Device->FlushParams(bDefault);
}

bool RF625DeviceNet::UDPConnect (USHORT UDPPort, String^ localIPAddress)
{
	const char* str = (const char*)(void*)Marshal::StringToHGlobalAnsi(localIPAddress);
	bool ret =  _rf625Device->UDPConnect(UDPPort, str);
	return ret;
}

bool RF625DeviceNet::UDPDisconnect()
{
	return _rf625Device->UDPDisconnect();
}

bool RF625DeviceNet::UDPGetResult(array<byte>^% buffer, bool OldMode)
{
	char* p = new char[ProfileBufferSize];
	bool ret = _rf625Device->UDPGetResult(p, OldMode);
	buffer = gcnew array<byte>(ProfileBufferSize);
	for (int i = 0; i < ProfileBufferSize; i ++)
	{
		buffer[i] = p[i];
	}
	return ret;
}

bool RF625DeviceNet::PingTest()
{
	return _rf625Device->PingTest();
}

UInt16 RF625DeviceNet::UDPPacketCounter(IntPtr buffer)
{
	return RF625Device::UDPPacketCounter((void*) buffer);
}

//USHORT RF625DeviceNet::UDPGetNormalizedResult(array<DWORD>^% pointsBuffer, USHORT% count)
//{
//	const int len = ProfileValuesCount + 2;
//	DWORD* poutMas = new DWORD[len];
//	USHORT* pCount = new USHORT;
//	USHORT res = _rf625Device->UDPGetNormalizedResult(poutMas, pCount);	
//	count = *pCount;
//	pointsBuffer = gcnew array<DWORD>(len);
//	for(int i = 0; i < len; i++)
//	{
//		pointsBuffer[i] = poutMas[i];
//	}
//	return res;
//}

USHORT RF625DeviceNet::ConvertResultToPoints(array<USHORT>^ resultBuffer, array<float>^% pointsBuffer, USHORT% count, BOOL ChecksumCheck)
{
	const int len = 2*1024 + 2;
	USHORT* pinMas = new USHORT[len];
	for (int i = 0; i < resultBuffer->Length; i ++)
	{
		pinMas[i] = resultBuffer[i];
	}
	float* poutMas = new float[2*1024];
	USHORT* pCount = new USHORT;
	USHORT res = _rf625Device->ConvertResultToPoints(pinMas, poutMas, pCount, ChecksumCheck);
	count = *pCount;
	pointsBuffer = gcnew array<float>(len);
	for(int i = 0; i < len; i++)
	{
		pointsBuffer[i] = poutMas[i];
	}
	return res;
}

USHORT RF625DeviceNet::GetNormalizedResult(array<float>^% pointsBuffer, USHORT% count)
{
	const int len = 2*1024;
	float* outMas = new float[len];
	USHORT* pCount = new USHORT;
	USHORT res = _rf625Device->GetNormalizedResult(outMas, pCount);
	count = *pCount;
	pointsBuffer = gcnew array<float>(len);
	for(int i = 0; i < len; i++)
	{		
		pointsBuffer[i] = outMas[i];
	}
	return res;
}

USHORT RF625DeviceNet::UDPGetNormalizedResult(array<float>^% pointsBuffer, USHORT% count)
{
	const int len = 2*1024;
	float* outMas = new float[len];
	USHORT* pCount = new USHORT;
	USHORT res = _rf625Device->UDPGetNormalizedResult(outMas, pCount);
	count = *pCount;
	pointsBuffer = gcnew array<float>(len);
	for(int i = 0; i < len; i++)
	{		
		pointsBuffer[i] = outMas[i];
	}
	return res;
}

DWORD RF625DeviceNet::GetSerialNumber()
{
	return _rf625Device->GetSerialNumber();
}

bool RF625DeviceNet::GetCapsExternalResultSync()
{
	return _rf625Device->GetCapsExternalResultSync();
}

bool RF625DeviceNet::GetCapsExternalMeasurementSync()
{
	return _rf625Device->GetCapsExternalMeasurementSync();
}

bool RF625DeviceNet::GetCapsAnalogueOutputByCurrent()
{
	return _rf625Device->GetCapsAnalogueOutputByCurrent();
}

bool RF625DeviceNet::GetCapsAnalogueOutputByVoltage()
{
	return _rf625Device->GetCapsAnalogueOutputByVoltage();
}

bool RF625DeviceNet::GetCapsLaserModulation()
{
	return _rf625Device->GetCapsLaserModulation();
}

WORD RF625DeviceNet::GetBaseDistance()
{
	return _rf625Device->GetBaseDistance();
}

WORD RF625DeviceNet::GetMeasurementRangeZ()
{
	return _rf625Device->GetMeasurementRangeZ();
}

WORD RF625DeviceNet::GetMeasurementRangeXSMR()
{
	return _rf625Device->GetMeasurementRangeXSMR();
}

WORD RF625DeviceNet::GetMeasurementRangeXEMR()
{
	return _rf625Device->GetMeasurementRangeXEMR();
}

DWORD RF625DeviceNet::GetFirmwareVersion()
{
	return _rf625Device->GetFirmwareVersion();
}

BYTE RF625DeviceNet::GetLaserLevel()
{
	return _rf625Device->GetLaserLevel();
}

WORD RF625DeviceNet::GetExposureTime()
{
	return _rf625Device->GetExposureTime();
}

WORD RF625DeviceNet::GetSignalProcessingTime()
{
	return _rf625Device->GetSignalProcessingTime();
}

BYTE RF625DeviceNet::GetLeftBoundary()
{
	return _rf625Device->GetLeftBoundary();
}

BYTE RF625DeviceNet::GetWindowWidth()
{
	return _rf625Device->GetWindowWidth();
}

BYTE RF625DeviceNet::GetUpperBoundary()
{
	return _rf625Device->GetUpperBoundary();
}

BYTE RF625DeviceNet::GetWindowHeight()
{
	return _rf625Device->GetWindowHeight();
}

bool RF625DeviceNet::GetExternalResultSyncEnabled()
{
	return _rf625Device->GetExternalResultSyncEnabled();
}

bool RF625DeviceNet::GetExternalResultSyncEdge()
{
	return _rf625Device->GetExternalResultSyncEdge();
}

bool RF625DeviceNet::GetExternalMeasurementSyncEnabled()
{
	return _rf625Device->GetExternalMeasurementSyncEnabled();
}

bool RF625DeviceNet::GetExternalMeasurementSyncEdge()
{
	return _rf625Device->GetExternalMeasurementSyncEdge();
}

bool RF625DeviceNet::GetAnalogueOutputSyncEnabled()
{
	return _rf625Device->GetAnalogueOutputSyncEnabled();
}

bool RF625DeviceNet::GetAnalogueOutputSyncEdge()
{
	return _rf625Device->GetAnalogueOutputSyncEdge();
}

bool RF625DeviceNet::GetAnalogueOutputTTL_OUT0Level()
{
	return _rf625Device->GetAnalogueOutputTTL_OUT0Level();
}

BYTE RF625DeviceNet::GetExternalSignalDivisor()
{
	return _rf625Device->GetExternalSignalDivisor();
}

BYTE RF625DeviceNet::GetMeasurementDelay()
{
	return _rf625Device->GetMeasurementDelay();
}

BYTE RF625DeviceNet::GetAnalogueOutputNumberOfPoints()
{
	return _rf625Device->GetAnalogueOutputNumberOfPoints();
}

BYTE RF625DeviceNet::GetAnalogueOutputPointHoldupTime()
{
	return _rf625Device->GetAnalogueOutputPointHoldupTime();
}

array<Byte>^ RF625DeviceNet::GetDeviceIPAddress()
{
	const byte * mas = _rf625Device->GetDeviceIPAddress();
	array<Byte>^ retMas = gcnew array<Byte>(4);
	retMas[0] = mas[0];
	retMas[1] = mas[1];
	retMas[2] = mas[2];
	retMas[3] = mas[3];
	return retMas;
}

array<Byte>^ RF625DeviceNet::GetHostIPAddress()
{
	const byte * mas = _rf625Device->GetHostIPAddress();
	array<Byte>^ retMas = gcnew array<Byte>(4);
	retMas[0] = mas[0];
	retMas[1] = mas[1];
	retMas[2] = mas[2];
	retMas[3] = mas[3];
	return retMas;
}

array<Byte>^ RF625DeviceNet::GetNetworkMask()
{
	const byte * mas = _rf625Device->GetNetworkMask();
	array<Byte>^ retMas = gcnew array<Byte>(4);
	retMas[0] = mas[0];
	retMas[1] = mas[1];
	retMas[2] = mas[2];
	retMas[3] = mas[3];
	return retMas;
}

String^ RF625DeviceNet::GetHumanReadableDeviceIPAddress()
{
	char * c = _rf625Device->GetHumanReadableDeviceIPAddress();
	String^ str = gcnew String(c);
	return str;
}

String^ RF625DeviceNet::GetHumanReadableHostIPAddress()
{
	char * c = _rf625Device->GetHumanReadableHostIPAddress();
	String^ str = gcnew String(c);
	return str;
}

String^ RF625DeviceNet::GetHumanReadableNetworkMask()
{
	char * c = _rf625Device->GetHumanReadableNetworkMask();
	String^ str = gcnew String(c);
	return str;
}

WORD RF625DeviceNet::GetHostPortNumber()
{
	return _rf625Device->GetHostPortNumber();
}

BYTE RF625DeviceNet::GetUDPFrequency()
{
	return _rf625Device->GetUDPFrequency();
}

short RF625DeviceNet::GetRotationAngle()
{
	return _rf625Device->GetRotationAngle();
}

bool RF625DeviceNet::SetLaserLevel(BYTE value)
{
	BYTE valueToSet = value;
	bool ret = _rf625Device->SetLaserLevel(valueToSet);
	return ret;
}

bool RF625DeviceNet::SetExposureTime(WORD value)
{
	return _rf625Device->SetExposureTime(value);
}

bool RF625DeviceNet::SetSignalProcessingTime(WORD value)
{
	return _rf625Device->SetSignalProcessingTime(value);
}

bool RF625DeviceNet::SetLeftBoundary(BYTE value)
{
	return _rf625Device->SetLeftBoundary(value);
}

bool RF625DeviceNet::SetWindowWidth(BYTE value)
{
	return _rf625Device->SetWindowWidth(value);
}

bool RF625DeviceNet::SetUpperBoundary(BYTE value)
{
	return _rf625Device->SetUpperBoundary(value);
}

bool RF625DeviceNet::SetWindowHeight(BYTE value)
{
	return _rf625Device->SetWindowHeight(value);
}

bool RF625DeviceNet::SetExternalResultSyncEnabled(bool value)
{
	return _rf625Device->SetExternalResultSyncEnabled(value);
}

bool RF625DeviceNet::SetExternalResultSyncEdge(bool value)
{
	return _rf625Device->SetExternalResultSyncEdge(value);
}

bool RF625DeviceNet::SetExternalMeasurementSyncEnabled(bool value)
{
	return _rf625Device->SetExternalMeasurementSyncEnabled(value);
}

bool RF625DeviceNet::SetExternalMeasurementSyncEdge(bool value)
{
	return _rf625Device->SetExternalMeasurementSyncEdge(value);
}

bool RF625DeviceNet::SetAnalogueOutputSyncEnabled(bool value)
{
	return _rf625Device->SetAnalogueOutputSyncEnabled(value);
}

bool RF625DeviceNet::SetAnalogueOutputSyncEdge(bool value)
{
	return _rf625Device->SetAnalogueOutputSyncEdge(value);
}

bool RF625DeviceNet::SetAnalogueOutputTTL_OUT0Level(bool value)
{
	return _rf625Device->SetAnalogueOutputTTL_OUT0Level(value);
}

bool RF625DeviceNet::SetExternalSignalDivisor(BYTE value)
{
	return _rf625Device->SetExternalSignalDivisor(value);
}

bool RF625DeviceNet::SetMeasurementDelay(BYTE value)
{
	return _rf625Device->SetMeasurementDelay(value);
}

bool RF625DeviceNet::SetAnalogueOutputNumberOfPoints(BYTE value)
{
	return _rf625Device->SetAnalogueOutputNumberOfPoints(value);
}

bool RF625DeviceNet::SetAnalogueOutputPointHoldupTime(BYTE value)
{
	return _rf625Device->SetAnalogueOutputPointHoldupTime(value);
}

bool RF625DeviceNet::SetDeviceIPAddress(array<Byte>^ value)
{
	BYTE bValue[4];
	bValue[0] = value[0];
	bValue[1] = value[1];
	bValue[2] = value[2];
	bValue[3] = value[3];
	return _rf625Device->SetDeviceIPAddress(bValue);
}

bool RF625DeviceNet::SetHostIPAddress(array<Byte>^ value)
{
	BYTE bValue[4];
	bValue[0] = value[0];
	bValue[1] = value[1];
	bValue[2] = value[2];
	bValue[3] = value[3];
	return _rf625Device->SetHostIPAddress(bValue);
}

bool RF625DeviceNet::SetNetworkMask(array<Byte>^ value)
{
	BYTE bValue[4];
	bValue[0] = value[0];
	bValue[1] = value[1];
	bValue[2] = value[2];
	bValue[3] = value[3];
	return _rf625Device->SetNetworkMask(bValue);
}

bool RF625DeviceNet::SetHostPortNumber(WORD value)
{
	return _rf625Device->SetHostPortNumber(value);
}

bool RF625DeviceNet::SetUDPFrequency(BYTE value)
{
	return _rf625Device->SetUDPFrequency(value);
}

bool RF625DeviceNet::SetRotationAngle(short value)
{
	return _rf625Device->SetRotationAngle(value);
}

_UDP_DEVINFOBLOCK_PC_Net^ RF625DeviceNet::GetDevBlock()
{
	RFDevice::_UDP_DEVINFOBLOCK_PC* dev = _rf625Device->GetDevBlock();
	_UDP_DEVINFOBLOCK_PC_Net^ ndev = gcnew _UDP_DEVINFOBLOCK_PC_Net(dev);
	return ndev;
}

void RF625DeviceNet::CloneParameters(RF625DeviceNet^% dev)
{
	dev = gcnew RF625DeviceNet(_rf625Device);
}

bool RF625DeviceNet::Reboot()
{
	return _rf625Device->Reboot();
}

bool RF625DeviceNet::PowerOff()
{
	return _rf625Device->PowerOff();
}

bool RF625DeviceNet::SetDevicePortNumber(WORD Port)
{
	return _rf625Device->SetDevicePortNumber(Port);
}

WORD RF625DeviceNet::GetDevicePortNumber()
{
	return _rf625Device->GetDevicePortNumber();
}

bool RF625DeviceNet::SetPixelBrightnessThreshold(BYTE Value)
{
	return _rf625Device->SetPixelBrightnessThreshold(Value);
}

BYTE RF625DeviceNet::GetPixelBrightnessThreshold()
{
	return _rf625Device->GetPixelBrightnessThreshold();
}

bool RF625DeviceNet::SetAutoExposureMode(BYTE Mode)
{
	return _rf625Device->SetAutoExposureMode(Mode);
}

BYTE RF625DeviceNet::GetAutoExposureMode()
{
	return _rf625Device->GetAutoExposureMode();
}

bool RF625DeviceNet::SetEnableLegacyMode(bool Enable)
{
	return _rf625Device->SetEnableLegacyMode(Enable);
}

bool RF625DeviceNet::GetEnabledLegacyMode()
{
	return _rf625Device->GetEnabledLegacyMode();
}

bool RF625DeviceNet::SetEnableRawImageMode(bool Enable)
{
	return _rf625Device->SetEnableRawImageMode(Enable);
}

bool RF625DeviceNet::GetEnabledRawImageMode()
{
	return _rf625Device->GetEnabledRawImageMode();
}

bool RF625DeviceNet::SetEnableInterpolation(bool Enable)
{
	return _rf625Device->SetEnableInterpolation(Enable);
}

bool RF625DeviceNet::GetEnabledInterpolation()
{
	return _rf625Device->GetEnabledInterpolation();
}

bool RF625DeviceNet::GetAutoExposure(LPWORD usValue)
{
	return _rf625Device->GetAutoExposure(usValue);
}


//bool RF625DeviceNet::SetEnableProfibusMode(bool Enable)
//{
//	return _rf625Device->SetEnableProfibusMode(Enable);
//}
//
//bool RF625DeviceNet::GetEnabledProfibusMode()
//{
//	return _rf625Device->GetEnabledProfibusMode();
//}